package com.gmrz.uaf.db.dao;

import com.gmrz.uaf.db.DAOException;
import com.gmrz.uaf.protocol.v1.processor.exception.UAFErrorCode;
import com.gmrz.util.Strings;
import com.gmrz.util.db.DBUtil;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by gongyuqiang on 2017/5/17.
 */
public class BaseServicePluginDAO implements ServicePluginDAO {
    private static final Logger LOG = LogManager
            .getLogger(BaseServicePluginDAO.class);
    private Connection conn_;

    private static final String _SQL_SELECT_SERVICE_PLUGIN_ALL = "SELECT aaid, class_name FROM t_service_plugin ";
    private static final String _SQL_SELECT_PLUGIN_CONFIG_ALL =
            "SELECT t_plugin_argument.config_cname, t_plugin_argument.config_value,t_plugin_argument.tenant_id from t_plugin_argument , t_service_plugin  ";
    private static final String _SQL_WHERE_UNION_AND_AAID = " WHERE t_plugin_argument.sp_id = t_service_plugin.sp_id and t_service_plugin.aaid = ?";

    @AssistedInject
    public BaseServicePluginDAO(@Assisted Connection conn_) {
        this.conn_ = conn_;
    }

    public BaseServicePluginDAO() {

    }

    @Override
    public Map<String, String> findAllPluginClasses() throws DAOException{
        String q = _SQL_SELECT_SERVICE_PLUGIN_ALL;
        PreparedStatement s = null;
        ResultSet rs = null;
        try {
            s = this.conn_.prepareStatement(q);
            rs = s.executeQuery();
            Map<String, String> map = new HashMap<String, String>();
            while (rs.next()) {

                String aaid = rs.getString("aaid");
                String classname = rs.getString("class_name");
                map.put(aaid, classname);
            }
            return map;
        } catch (SQLException sqle) {
            LOG.error("The following SQL statement failed to execute:\n {}",
                    q,sqle);
            throw new DAOException(UAFErrorCode.DB_QUERY_AUTHENTICATOR_FAILED, sqle);
        } finally {
            DBUtil.close(rs);
            DBUtil.close(s);
        }
    }

    @Override
    public Map<String, String> findAllPluginConfig(String aaid) throws DAOException {
        String q = _SQL_SELECT_PLUGIN_CONFIG_ALL + _SQL_WHERE_UNION_AND_AAID;
        PreparedStatement s = null;
        ResultSet rs = null;
        try {
            s = this.conn_.prepareStatement(q);
            s.setString(1, aaid);
            rs = s.executeQuery();
            Map<String, String> map = new HashMap<String, String>();
            while (rs.next()) {

                String configName = rs.getString("config_cname");
                String configValue = rs.getString("config_value");
                String tenant_id = rs.getString("tenant_id");
                if(Strings.isNotBlank(tenant_id)){
                    map.put(configName+"."+tenant_id, configValue);
                }else {
                    map.put(configName, configValue);
                }
            }
            return map;
        } catch (SQLException sqle) {
            LOG.error("The following SQL statement failed to execute:\n {}",
                    q,sqle);
            throw new DAOException(UAFErrorCode.DB_QUERY_AUTHENTICATOR_FAILED, sqle);
        } finally {
            DBUtil.close(rs);
            DBUtil.close(s);
        }
    }
}
